import wepy from 'wepy'
/**
 * 提示与加载工具类
 */
export default class Tips {
  static isLoading = false;
  /**
   * 弹出提示框
   */

  static success (title, duration = 1000) {
    return wepy.showToast({
      title: title,
      icon: 'success',
      mask: true,
      duration: duration
    })
  }

  /**
   * 弹出确认窗口
   */
  static modal (text, title = '提示') {
    return wepy.showModal({
      title: title,
      content: text,
      confirmColor: '#ffd200',
      showCancel: false
    })
  }

  /**
   * 弹出确认窗口
   */
  static async confirm (text, title = '提示') {
    const status = await wepy.showModal({
      title: title,
      content: text,
      confirmColor: '#ffd200',
      showCancel: true
    })
    if (status.cancel) {
      return false
    }
    return true
  }

  /**
   * 警告框
   */
  static alert (title, duration = 1000) {
    return wepy.showToast({
      title: title,
      image: '/assets/icons/alert.png',
      mask: true,
      duration: duration
    })
  }

  /**
   * 错误框
   */

  static error (title, duration = 1000) {
    return wepy.showToast({
      title: title,
      image: '/assets/icons/error.png',
      mask: true,
      duration: duration
    })
  }

  /**
   * 弹出加载提示
   */
  static loading (title = '加载中', mask = true, force = false) {
    if (this.isLoading && !force) {
      return
    }
    this.isLoading = true
    wepy.showLoading({
      title,
      mask
    })
    wepy.showNavigationBarLoading()
  }

  /**
   * 加载完毕
   */
  static loaded () {
    if (this.isLoading) {
      this.isLoading = false
      wepy.hideLoading()
      wepy.hideNavigationBarLoading()
      wepy.stopPullDownRefresh()
    }
  }

  /**
   * 弹出下拉动作栏
   */
  static async actions (...items) {
    const tap = await wepy.showActionSheet({
      itemList: items
    })
    const result = {
      index: tap.tapIndex,
      text: items[tap.tapIndex]
    }
    return result
  }
}
